package cn.wolfcode.wms.controller;

import cn.wolfcode.wms.domain.StockOutcomeBill;
import cn.wolfcode.wms.qo.StockOutcomeBillQueryObject;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.IDepotService;
import cn.wolfcode.wms.service.IStockOutcomeBillService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/stockOutcomeBill")
public class StockOutcomeBillController {

    @Autowired
    private IStockOutcomeBillService stockOutcomeBillService;
    @Autowired
    private IDepotService depotService;
    @Autowired
    private IClientService clientService;

    @RequestMapping("/list")
    @RequiredPermission("出库订单列表")
    public String list(@ModelAttribute("qo") StockOutcomeBillQueryObject qo, Map map) {
        map.put("depots", depotService.listAll());
        map.put("clients", clientService.listAll());
        map.put("pageResult", stockOutcomeBillService.query(qo));
        return "stockOutcomeBill/list";
    }

    @RequestMapping("/input")
    @RequiredPermission("出库订单编辑")
    public String input(Long id, Map map) {
        map.put("depots", depotService.listAll());
        map.put("clients", clientService.listAll());
        if (id != null) {
            map.put("stockOutcomeBill", stockOutcomeBillService.getById(id));
        }
        return "stockOutcomeBill/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("更新或新增出库订单")
    public String saveOrUpdate(StockOutcomeBill stockOutcomeBill) {
        try {
            stockOutcomeBillService.saveOrUpdate(stockOutcomeBill);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/stockOutcomeBill/list.do";
    }

    @RequestMapping("/delete")
    @RequiredPermission("删除出库订单")
    public String delete(Long id) {
        stockOutcomeBillService.delete(id);
        return "redirect:/stockOutcomeBill/list.do";
    }

    @RequestMapping("/view")
    @RequiredPermission("查看出库订单")
    public String view(Long id, Map map) {
        map.put("stockOutcomeBill", stockOutcomeBillService.getById(id));
        return "/stockOutcomeBill/view";
    }

    @RequestMapping("/audit")
    @RequiredPermission("审核出库订单")
    @ResponseBody
    public JSONResult audit(Long id) {
        try {

            stockOutcomeBillService.audit(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("审核成功");
    }
}
